<template>
  <div class="container">
    <el-dialog
      :title="titleInfo.text + titleInfo.pageTitle"
      width="450px"
      :visible.sync="dialogFormVisible"
      @close="handleClose"
    >
      <el-form
        ref="form"
        :model="formData"
        :rules="ruleInline"
        label-width="80px"
      >
        <el-form-item label="所属学科" prop="subjectID" v-show="isShow">
          <el-select
            v-model="formData.subjectID"
            placeholder="请选择"
            style="width:300px; padding-bottom:30px"
          >
            <el-option
              v-for="item in simpleList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName">
          <el-input
            v-model="formData.tagName"
            style="width:300px"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="CloseEvent">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  props: {
    titleInfo: {
      type: Object,
      required: true
    },
    formData: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      isShow: true,
      dialogFormVisible: false,
      simpleList: [{ label: '' }],
      // 表单验证
      ruleInline: {
        subjectID: [
          { required: true, message: '所属学科不能为空', trigger: 'blur' }
        ],
        tagName: [
          { required: true, message: '目录不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 弹层显示
    dialogFormV () {
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogFormH () {
      this.dialogFormVisible = false
    },
    CloseEvent () {
      this.$refs.form.resetFields()
      this.dialogFormVisible = false
    },
    // 关闭
    handleClose () {
      this.dialogFormVisible = false
    },
    // 提交
    submit () {
      this.$refs.form.validate(async res => {
        if (res) {
          if (this.titleInfo.text === '创建') {
            await add(this.formData)
            this.$message.success('新增成功')
          } else {
            await update(this.formData)
            this.$message.success('编辑成功')
          }
          this.$emit('gitList')
          this.CloseEvent()
          this.dialogFormVisible = false
        }
      })
    }
  },
  async created () {
    const res = await simple()
    this.simpleList = res.data
    // console.log(res.data, '12')
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-dialog__footer {
  text-align: right;
}
::v-deep .el-input--suffix .el-input__inner {
  padding-right: 69px;
}
</style>
